@tool
class_name EagleStateChecker
extends Node2D

#region 索敌
var is_player_detected: bool = false

##
var player_position: Vector2 = Vector2.ZERO

## 旋转速度（度/秒）
var rotation_speed: float = 120.0

## 当前旋转角度
var current_angle: float = 0.0

## 最小旋转角度
var min_angle: float = -90.0

## 最大旋转角度
var max_angle: float = 0.0

## 旋转方向（1为向下旋转，-1为向上旋转）
var direction: int = 1
#endregion

##
var is_ground_detected: bool = false

##
@onready var player_checker: RayCast2D = %PlayerChecker

##
@onready var ground_checker: Area2D = %GroundChecker


func on_update(delta: float) -> void:
	# 更新 player_checker 的角度
	current_angle += direction * rotation_speed * delta
	
	# player_checker 的限制角度
	if current_angle >= max_angle:
		current_angle = max_angle
		direction = -1
	elif current_angle <= min_angle:
		current_angle = min_angle
		direction = 1
	
	# 设置 player_checker 的旋转
	player_checker.rotation_degrees = current_angle
	
	var player = player_checker.get_collider() as Player
	if player:
		player_position = player.global_position

	is_player_detected = player != null
	
	is_ground_detected = ground_checker.get_overlapping_bodies().size() > 0


func _process(delta: float) -> void:
	on_update(delta)
